/*
 * @Author: zhangyang
 * @Date: 2024-05-23 14:37:25
 * @LastEditTime: 2024-06-04 15:53:32
 * @Description: 
 */
import 'package:flutter/material.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import '/database/bill.dart';
import '/util/log_utils.dart';

class YoungDashboard extends StatefulWidget {
  const YoungDashboard({super.key});

  @override
  State<StatefulWidget> createState() => YoungDashboardState();
}

class YoungDashboardState extends State<YoungDashboard> {
  String _year = '';
  String _month = '';

  bool _visible = false;

  double _outcome = 0;
  double _income = 0;

  @override
  void initState() {
    super.initState();

    DateTime now = DateTime.now();
    _year = now.year.toString();
    _month = now.month.toString().padLeft(2, '0');

    getBillList();

    _visible = false;

    _outcome = 123.45;
    _income = 12345.67;
  }

  Future getBillList() async {
    final res =
        await DataBaseBill.getMonthBill(int.parse(_year), int.parse(_month));

    setState(() {
      _outcome = res['outcome'];
      _income = res['income'];
    });
  }

  String displayMoney(num n) {
    return '¥ ${_visible ? n.toStringAsFixed(2) : "***"}';
  }

  @override
  Widget build(BuildContext context) {
    return Card(
        color: Colors.blueAccent,
        shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(14)),
        child: ConstrainedBox(
          constraints:
              const BoxConstraints(minWidth: double.infinity, minHeight: 170),
          child: Container(
            padding: const EdgeInsets.all(10),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    GestureDetector(
                      onTap: () {
                        TDPicker.showDatePicker(context,
                            title: '请选择时间',
                            useDay: false,
                            dateStart: [2024, 01, 01],
                            initialDate: [
                              _year.isEmpty ? 2024 : int.parse(_year),
                              _month.isEmpty ? 05 : int.parse(_month),
                              01
                            ],
                            pickerHeight: 240, onConfirm: (date) {
                          Log.d(date.toString());

                          setState(() {
                            _year = date['year'].toString();
                            _month = date['month'].toString().padLeft(2, '0');
                            Log.d('$_year年$_month月');
                          });

                          getBillList();

                          // 关闭弹窗
                          Navigator.of(context).pop();
                        });
                      },
                      child: Row(
                        children: [
                          Text('$_year年$_month月',
                              style: const TextStyle(
                                  color: Colors.white,
                                  fontSize: 14,
                                  fontWeight: FontWeight.bold)),
                          const Icon(
                            Icons.arrow_drop_down_outlined,
                            color: Colors.white,
                          )
                        ],
                      ),
                    ),
                    GestureDetector(
                      onTap: () async {
                        if (!_visible) {
                          // todo: 指纹 / 人脸认证
                          Log.d('todo: add auth');
                        }

                        setState(() {
                          _visible = !_visible;
                        });
                      },
                      child: Icon(
                        _visible ? TDIcons.browse : TDIcons.browse_off,
                        color: Colors.white,
                        size: 28,
                      ),
                    ),
                  ],
                ),
                const SizedBox(height: 16),
                const Text('总支出',
                    style: TextStyle(color: Colors.white, fontSize: 14)),
                Text('¥ ${_outcome.toStringAsFixed(2)}',
                    style: const TextStyle(
                        color: Colors.white,
                        fontSize: 32,
                        fontWeight: FontWeight.bold)),
                const SizedBox(height: 16),
                Row(
                  children: [
                    Text(
                      '总收入: ${displayMoney(_income)}',
                      style: const TextStyle(color: Colors.white, fontSize: 14),
                    ),
                    const Text('   |   ',
                        style: TextStyle(color: Colors.white, fontSize: 14)),
                    Text(
                      '结余: ${displayMoney(_income - _outcome)}',
                      style: const TextStyle(color: Colors.white, fontSize: 14),
                    )
                  ],
                )
              ],
            ),
          ),
        ));
  }
}
